package com.changgou.business.listener;

import com.changgou.business.config.RabbitMQConfig;
import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 广告表的监听器,tb_ad表进行了修改,发送消息到消息队列中.
 *      监听 ad_update_queue 消息队列
 */
@Component
public class AdListener {

    @RabbitListener(queues = RabbitMQConfig.AD_UPDATE_QUEUE)
    public void receiveMessage(String message){
        System.out.println("接收的消息 : "+message);
        //更新数据到缓存中,openresty中
        // http://虚拟机IP/ad_update?position=message

        //后端发送http请求,okHttp,客户端发送http请求的对象
        OkHttpClient okHttpClient = new OkHttpClient();
        String url = "http://192.168.200.128/ad_update?position="+message;
        Request request = new Request.Builder().url(url).build();
        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //请求发送失败的回调方法.
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //请求成功的回调方法
                System.out.println("请求成功 : "+response.message());
            }
        });
    }
}
